﻿////////////////////////////////////////////////////////////////////////////////////////////////////
// <copyright file="AbstractModListBackup.cs" company="SingularityShift.com">
// Copyright (c) 2010 SingularityShift.com. All rights reserved.
// </copyright>
// <author>Ben McClure</author>
// <email>ben@singularityshift.com</email>
// <date>11/19/2010</date>
// <summary>Implements the abstract mod list backup class</summary>
////////////////////////////////////////////////////////////////////////////////////////////////////
namespace GameLib.Backups
{
    using GameLib.Games;

    ////////////////////////////////////////////////////////////////////////////////////////////////////
    /// <summary>   Abstract mod list backup. </summary>
    ///
    /// <remarks>   Ben McClure, 11/19/2010. </remarks>
    ////////////////////////////////////////////////////////////////////////////////////////////////////
    public abstract class AbstractModListBackup : AbstractBackup, IModListBackup
    {
        #region Fields

        /// <summary> The mods </summary>
        private IModCollection<IMod> _mods;
        /// <summary> The game </summary>
        private IGame _game;

        #endregion Fields

        #region Properties

        ////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>   Gets or sets the game. </summary>
        ///
        /// <value> The game. </value>
        ////////////////////////////////////////////////////////////////////////////////////////////////////
        public IGame Game
        {
            get { return _game; }
            protected set { _game = value; }
        }

        ////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>   Gets or sets the mods. </summary>
        ///
        /// <value> The mods. </value>
        ////////////////////////////////////////////////////////////////////////////////////////////////////
        public IModCollection<IMod> Mods
        {
            get
            {
                return _mods;
            }
            protected set
            {
                _mods = value;
            }
        }

        #endregion Properties

        #region AbtractBackup Member Overrides

        ////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>   Backups. </summary>
        ///
        /// <param name="writer">   The writer. </param>
        ///
        /// <returns>   Number of items backed up, if applicable. </returns>
        ////////////////////////////////////////////////////////////////////////////////////////////////////
        public override int Backup(IBackupWriter writer)
        {
            writer.Write(this);
            if (writer.Count >= 1)
            {
                return Mods.Count;
            }
            else
            {
                return writer.Count;
            }
        }

        ////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>   Restores. </summary>
        ///
        /// <param name="reader">   The reader. </param>
        ///
        /// <returns>   Number of items backed up, if applicable. </returns>
        ////////////////////////////////////////////////////////////////////////////////////////////////////
        public override int Restore(IBackupReader reader)
        {
            IModListBackup restored = (IModListBackup)reader.Read(this);
            OutputPath = restored.OutputPath;
            Created = restored.Created;
            restored.Mods.SetOwner(Game);
            foreach (IMod mod in restored.Mods)
            {
                mod.SetOwner(Game);
            }

            Mods = restored.Mods;

            return Mods.Count;
        }

        #endregion AbtractBackup Member Overrides
    }
}